Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify SPDX compound expressions #9360

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

sschuberth
Copy link
Member

@sschuberth sschuberth commented Oct 31, 2024

Please have a look at the individual commit messages for the details.

Copy link

codecov bot commented Oct 31, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 67.67%. Comparing base (bfcfe62) to head (c9048f3).

Additional details and impacted files
@@            Coverage Diff            @@
##               main    #9360   +/-   ##
=========================================
  Coverage     67.67%   67.67%           
  Complexity     1223     1223           
=========================================
  Files           244      244           
  Lines          8626     8626           
  Branches        911      911           
=========================================
  Hits           5838     5838           
  Misses         2413     2413           
  Partials        375      375           
Flag Coverage Δ
funTest-docker 62.08% <ø> (ø)
funTest-non-docker 33.57% <ø> (ø)
test 37.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Improve the overview before adding more tests.

Signed-off-by: Sebastian Schuberth <[email protected]>
Do not even create a copy if operands are equal. Resolves #8714.

Signed-off-by: Sebastian Schuberth <[email protected]>
@sschuberth sschuberth marked this pull request as ready for review November 5, 2024 15:40
@sschuberth sschuberth requested a review from a team as a code owner November 5, 2024 15:40

/**
* Concatenate [this][SpdxExpression] and [other] using [SpdxOperator.OR].
*/
infix fun or(other: SpdxExpression) =
takeIf { this == other } ?: SpdxCompoundExpression(SpdxOperator.OR, listOf(this, other))
takeIf { this == other } ?: SpdxCompoundExpression(SpdxOperator.OR, setOf(this, other))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the takeIf expression the setOf here and above should not have any effect except for unnecessarily repeating the comparison.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll simply drop this commit to address this and the below issue.

@@ -224,14 +224,14 @@ class SpdxCompoundExpression(
* Create a compound expression with the provided [operator] and the [left] and [right] child expressions.
*/
constructor(left: SpdxExpression, operator: SpdxOperator, right: SpdxExpression) :
this(operator, listOf(left, right))
this(operator, setOf(left, right))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could run into an exception as SpdxCompoundExpression requires that children.size > 1. So basically this change now forbids to make compound expressions with equals operands.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll simply drop this commit to address this and the above issue.

@@ -34,4 +35,37 @@ class SpdxCompoundExpressionTest : WordSpec({
}
}
}

"Simplifying a compound expression" should {
"nested children of the same operator" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Word missing in the beginning.

}
}

return SpdxCompoundExpression(operator, flattenedChildren)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this function should be called by normalize as well. Also, this will throw an exception if flattenedChildren.size < 2.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this function should be called by normalize as well.

I have a slight tendency towards keeping normalizing and simplifying separate use-cases.

However, having the respective File functions in mind, we should have probably called the current normalize rather canonize, leaving room for calling this function normalize instead of simplify.

Also, this will throw an exception if flattenedChildren.size < 2.

Good find. Fixed, and added a test for that case.

@@ -204,6 +204,7 @@ internal fun Package.toSpdxPackage(
SpdxConstants.NONE
} else {
packageLicenseExpressions.reduce(SpdxExpression::and)
.simplify()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will now throw an exception if the input contains only two identical licenses.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, see my comment above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants